﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Runtime.InteropServices;
using project_assess.read_excel;
using project_assess.assess_relate;
using project_assess.prj_view;
using System.Diagnostics;



namespace project_assess
{
    public partial class Mfrm :Office2007RibbonForm
    {
        #region 全局变量
        public DataBase m_db = null;
        [DllImport("kernel32.dll")]
        public static extern int GetPrivateProfileString(string section, string key,
              string def, StringBuilder retval, int size, string filepath);
        [DllImport("user32.dll", EntryPoint = "FindWindowExA", 
            CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        private static extern IntPtr FindWindowEx(int hwndParent, 
            int hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
        public string curprjid;//当前的项目编号
        #endregion
        public Mfrm()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            Connect_To_DataBase();
            if (m_db == null)
            {
                message_frm frm = new message_frm("无法连接到数据库,\r\n请查看网络连接.",
                    "错误", icon_img.error);
                frm.ShowDialog();
                frm.Close();
                return;
            }
        }

        void Mfrm_prjid_change(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        //连接数据库
        private void Connect_To_DataBase()
        {
            DataBase db = null;
            string computer = System.Environment.MachineName;
            StringBuilder str = new StringBuilder(40);
            string ip, database, user, psw, port;
            string def = "";
            string path = System.Windows.Forms.Application.StartupPath + @"\config.ini";
            GetPrivateProfileString("config", "ip", def, str,
                40, path);
            ip = str.ToString();
            GetPrivateProfileString("config", "port", def, str, 40, path);
            port = str.ToString();
            GetPrivateProfileString("config", "database", def, str, 40, path);
            database = str.ToString();
            GetPrivateProfileString("config", "user", def, str, 40, path);
            user = str.ToString();
            GetPrivateProfileString("config", "psw", def, str, 40, path);
            psw = str.ToString();
            if (DataBase.ConnectToDataBase("sqlserver", ip, port, "",
                     computer + "\\SQLEXPRESS", user, psw, database, false, ref db))
            {
                m_db = db;
            }
        }

        //所有按钮操作
        //所有的按钮操作
        private void btn_Click(object sender, EventArgs e)
        {
            //Process cal = Process.Start("calc.exe");
            //IntPtr p = FindWindowEx(cal.MainWindowHandle.ToInt32(), 0, "#32770", null);
            //string w = "";
            //IntPtr ptr = Marshal.StringToHGlobalAnsi(w);
            //if (SendMessage(p, 0xD, 100, ptr) == 1)
            //{
            //    string re = Marshal.PtrToStringAnsi(p);
            //}
            try
            {
                ButtonItem item = sender as ButtonItem;
                switch (item.Tag.ToString())
                {
                    case "项目测算":
                        {
                            read_excel();
                            break;
                        }
                    case "材料埋设":
                        {
                            material_bury_assess();
                            break;
                        }
                    case "控制成本":
                        {
                            predict_assess();
                            break;
                        }
                    case "管理考核":
                        {
                            manamge_assess();
                            break;
                          
                        }
                    case "核定工作量":
                        {
                            work_assess();
                            break;
                        }
                    case "项目查看":
                        {
                            show_prj(); 
                            break;
                        }
                    case "结束考核":
                        {
                            finish_prj(); 
                            break;
                        }
                    case "参数修改":
                        {
                            para_change();
                            break;
                        }
                    case "项目修改":
                        {
                            prj_change();
                            break;
                        }
                    case "单价修改":
                        {
                            price_change();
                            break;
                        }
                    case "项目信息":
                        {
                            show_prjinfo();
                            break;
                        }
                    case "选择工程":
                        {
                            choose_prj frm = new choose_prj(this);
                            frm.ShowDialog(this);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            catch
            {
                return;
            }
        }

        private void price_change()
        {
            ratio_frm frm = new ratio_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        public read_Excel_frm read_excel()
        {
            read_Excel_frm frm = new read_Excel_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
            return frm;
        }
        void material_bury_assess()
        {
            material_frm frm = new material_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void predict_assess()
        {
            predict_frm frm = new predict_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void manamge_assess()
        {
            manage_frm frm = new manage_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void work_assess()
        {
            work_frm frm = new work_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        void show_prj()
        {
            prjview_frm frm = new prjview_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void show_prjinfo()
        {
            prjinfo_frm frm = new prjinfo_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void finish_prj()
        {
            finish_frm frm = new finish_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void para_change()
        {
            para_frm frm = new para_frm(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        void prj_change()
        {
            prj_change frm = new prj_change(this);
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        #region 消息显示
        //时间触发器
        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            Label.Text = "";
        }
        //mfrm消息
        public void msg(string txt)
        {
            Label.Text = txt;
            timer.Start();
        }
        #endregion


        public double get_para(string name)
        {
            string para_database;
            para_database = get_para_database();
            string sql = "select [参数值] from [" + para_database + "] where [参数名]='" + name + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            return Convert.ToDouble(dt.Rows[0][0]);
        }

        public string get_para_database()
        {
            string para_database;
            if (curprjid == null || curprjid == "")
            {
                para_database = "parameter";
            }
            else
            {
                para_database = curprjid + "_para";
            }
            if (m_db.IsTableInDataBase(para_database) == false)
            {
                para_database = "parameter";
            }
            return para_database;
        }

        //加载登录界面
        private void Mfrm_Load(object sender, EventArgs e)
        {
            loginfrm frm = new loginfrm(m_db);
            frm.ShowDialog();
            if (frm.pass == false)
            {
                frm.Close();
                message_frm msg = new message_frm("用户密码不正确", "无法登录", icon_img.info);
                msg.StartPosition = FormStartPosition.CenterScreen;
                msg.ShowDialog();
                msg.Close();
                this.Close();
            }
            else//待添加
            {
 
            }
        }
    }
}
